package com.ag.hmk.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.ag.bean.TbDisposalAdvice;
import com.ag.bean.TbEvent;
import com.ag.bean.TbExpert;
import com.ag.bean.TbUser;
import com.ag.common.BaseAction;
import com.ag.common.IDUtil;
import com.ag.common.PageUtil;
import com.ag.hmk.service.impl.HmkUiltServImpl;
/**
 * 
 * @author hmk
 * 查询专家列表（分页查询）
 */
public class FindSomeExpertAction extends BaseAction {
	private static final long serialVersionUID = -212804922666399629L;
	
	private HmkUiltServImpl hmkUiltServImpl;
	private int page;
	private int maxRow;
	private String typeID1;
	private String areaID1;
	private String expName1;
	
	private PageUtil pageUtil; //返回结果
	private List<String> eventList; //专家参与的事件处置
	
	/*
	 * 查询专家列表（分页查询）
	 */
	public String findExpertList()throws Exception{
		List<String> eList = new ArrayList<String>();
		PageUtil pUtil = new PageUtil();
		List result=null;
		int total=0;
		try {
			String cql = "select count(exp.expId) from TbExpert exp where 1=1";
			String hql = "from TbExpert exp left join fetch exp.tbType left join fetch exp.tbExpertArea " +
					" left join fetch exp.tbUser u  left join fetch u.tbUserState  " +
					" left join fetch u.tbDisposalAdvicesForAdviceUId  da  left join fetch da.tbEvent  where 1=1 ";
			System.out.println("----"+typeID1);
			if (typeID1!=null && typeID1.length()>0) {
				cql += " and exp.tbType.tpyeId = " + typeID1;
				hql += " and exp.tbType.tpyeId = " + typeID1;
			}
			
			System.out.println("----"+areaID1);
			if (areaID1!=null && areaID1.length()>0) {
				cql += " and exp.tbExpertArea.expareaId = " + areaID1;
				hql += " and exp.tbExpertArea.expareaId = " + areaID1;
			}
			
			System.out.println("----"+expName1);
			if (expName1!=null && expName1.length()>0) {
				cql += " and exp.expName like '%" + expName1+"%'";
				hql += " and exp.expName like '%" + expName1+"%'";
			}
			cql+=" and exp.a='"+IDUtil.PRESERVE+"' ";
			hql+=" and exp.a='"+IDUtil.PRESERVE+"' order by exp.expId desc";

			if (page == 0) {
				page = 1;
			}
			if (maxRow == 0) {
				maxRow = 10;
			}
			int firstRow = (page - 1) * maxRow;
			result = hmkUiltServImpl.query(hql, firstRow, maxRow);
		
			//参与事件处置 处理开始
			if (result!=null) {
				total = result.size();
				Iterator it1 = result.iterator();
				while(it1.hasNext()){
					String str1 = "";
					int i = 0;
					TbExpert expert2 =(TbExpert) it1.next();
					TbUser user2 = expert2.getTbUser();
					if (user2!=null) {
						
						Set<TbDisposalAdvice> daSet = user2.getTbDisposalAdvicesForAdviceUId();
						if (!daSet.isEmpty()) {
							str1="参与了";
							Iterator<TbDisposalAdvice> it2 = daSet.iterator();
							while(it2.hasNext()){
								TbDisposalAdvice dAdvice = it2.next();
								//TbNotifytoexper ne = dAdvice.getTbNotifytoexper();
								
								if(dAdvice!=null){
									TbEvent event1 = dAdvice.getTbEvent();
									if (event1!=null) {
										i++;
										str1 +=(event1.getEEventname()+"、");
									} 
								}
								if (i>2) {
									break;
								}
								
							} 
							//str1 = str1.substring(0, 20);
							str1+="...事件处置；";
						
						}else{
							str1="没有参与事件处置";
						}
					} else { //user2为空
						str1="没有参与事件处置";
					} 
					
					eList.add(str1);
				}
			}
			//参与事件处置 处理结束
		} catch (Exception e) {
			e.printStackTrace();
			result=null;
			total=0;
			eList = null;
		}

		pUtil.setTotal(total);
		pUtil.setPage(page);
		pUtil.setMaxRow(maxRow);
		pUtil.setList(result);
		
		this.setPageUtil(pUtil);
		this.setEventList(eList);
		return "findExpertListOK";
	}

	public PageUtil getPageUtil() {
		return pageUtil;
	}

	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}

	public void setHmkUiltServImpl(HmkUiltServImpl hmkUiltServImpl) {
		this.hmkUiltServImpl = hmkUiltServImpl;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setMaxRow(int maxRow) {
		this.maxRow = maxRow;
	}

	public void setTypeID1(String typeID1) {
		this.typeID1 = typeID1;
	}

	public void setAreaID1(String areaID1) {
		this.areaID1 = areaID1;
	}

	public void setExpName1(String expName1) {
		this.expName1 = expName1;
	}

	public List<String> getEventList() {
		return eventList;
	}

	public void setEventList(List<String> eventList) {
		this.eventList = eventList;
	}

	

}
